package com.ws.framework.zk;

import java.util.Objects;

import org.apache.curator.shaded.com.google.common.base.Strings;

import com.google.common.base.Charsets;
import com.google.common.base.MoreObjects;

public class Auther {

	private String username;

	private String password;

	/**
	 * @param username
	 * @param password
	 */
	public Auther(String username, String password) {
		this.username = username;
		this.password = password;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	
	public byte[] toAuth() {
		return (Strings.nullToEmpty(username) + ":" + Strings.nullToEmpty(password)).getBytes(Charsets.UTF_8);
	}

	@Override
	public String toString() {
		return MoreObjects.toStringHelper(this).add("name", this.getUsername()).add("password", this.getPassword())
				.toString();
	}

	@Override
	public boolean equals(Object obj) {
		if (Objects.isNull(obj) || !(obj instanceof Auther))
			return false;
		Auther auther = (Auther) obj;
		return auther.getUsername().equals(this.getUsername()) && auther.getPassword().equals(this.getPassword());
	}

	@Override
	public int hashCode() {
		int result = username != null ? username.hashCode() : 0;
		result = 31 * result + (password != null ? password.hashCode() : 0);
		return result;
	}
}